package com.zkingsoft.thread;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.dingtalk.open.client.api.model.corp.MessageBody;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Body;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Body.Form;
import com.dingtalk.open.client.api.model.corp.MessageBody.OABody.Head;
import com.dingtalk.open.client.api.model.corp.MessageType;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.dingding.Env;
import com.zkingsoft.dingding.Vars;
import com.zkingsoft.dingding.auth.AuthHelper;
import com.zkingsoft.dingding.message.LightAppMessageDelivery;
import com.zkingsoft.dingding.message.MessageHelper;
import com.zkingsoft.model.Changes;
import com.zkingsoft.model.ProjUser;
import com.zkingsoft.model.Project;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.services.ChangesService;
import com.zkingsoft.services.ProjectService;

/**
 * 发送与客户问题反馈的消息通知线程类
 * 
 * @author Administrator
 *
 */
public class SendFeedbackOaMsgsThread implements Runnable {

	ProjectService projectService;
	ChangesService changesService;
	Changes changes = null;
	String bgColor = null;
	String title = null;	
	
	public SendFeedbackOaMsgsThread(ProjectService projectService, ChangesService changesService, 
			Changes changes, String bgColor, String title) {
		super();
		this.projectService = projectService;
		this.changesService = changesService;
		this.changes = changes;
		this.bgColor = bgColor;
		this.title = title;
	}

	@Override
	public void run() {
		sendFeedbackOaMsgs();
	}

	public void sendFeedbackOaMsgs() {
		try {
			//查询项目经理的id
			Project proj = projectService.findById(changes.getProjId());
			List<ProjUser> projUserList = proj.getProjUserList();
			String ddUser = "";
			for(ProjUser projUser : projUserList){
				String postName = projUser.getPostName();
				if(Dictionary.POST_NAME_XMJL.equals(postName)){
					ddUser = projUser.getDdUser();
				}
			}
			Changes change = changesService.findUserCodeByChangesId(changes.getId());
			if(ddUser!=null&& !"".equals(ddUser)){
				String accessToken = AuthHelper.getAccessToken();// 获取accessToken
//				String toUsers = change.getDdUserId();
				String toParties = Vars.TO_PARTY;
				String agentId = Vars.AGENT_ID;
				// 创建oa消息
				MessageBody.OABody oaBody = createOaMsg(change);
				// 发送微应用消息
				LightAppMessageDelivery lightAppMessageDelivery = new LightAppMessageDelivery(ddUser, toParties, agentId);
				lightAppMessageDelivery.withMessage(MessageType.OA, oaBody);
				MessageHelper.send(accessToken, lightAppMessageDelivery);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * @Description:创建OA消息
	 * @author:dingchuan
	 * @param task
	 * @return 返回类型 MessageBody.OABody
	 * @date 2016年12月6日
	 */
	public MessageBody.OABody createOaMsg(Changes changes) {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FORMAT_DD);
		MessageBody.OABody oaBody = new MessageBody.OABody();
		oaBody.setMessage_url(Env.LOOK_FEEDBACK_MESSAGE + changes.getId());
		oaBody.setPc_message_url(Env.LOOK_FEEDBACK_MESSAGE + changes.getId());
		// 头部
		Head head = new Head();
		head.setText("");
		head.setBgcolor(bgColor);
		oaBody.setHead(head);
		// 内容
		Body body = new Body();
		body.setTitle(changes.getTitle());//反馈的标题
		List<Form> formList = new ArrayList<Form>();
		Form form1 = new Form();
		form1.setKey("类型");
		form1.setValue(this.title);
		formList.add(form1);
		Form form2 = new Form();
		form2.setKey("所属项目");
		form2.setValue(changes.getProjName());
		formList.add(form2);
		Form form3 = new Form();
		form3.setKey("反馈时间");
		form3.setValue(sdf.format(changes.getCreateTime()));
		formList.add(form3);
		Form form4 = new Form();// 反馈人
		form4.setKey("反馈人");
		form4.setValue(changes.getCreateClientName());
		formList.add(form4);
		body.setForm(formList);
		oaBody.setBody(body);
		return oaBody;
	}

}
